<script type="text/javascript" src="<?= base_url() . 'resource/js/layer/layer.js' ?>" charset="utf-8"></script>
<!--top结束-->
<style type="text/css">
    .perssiom {
        margin: 10px 0px;
    }

    .perssiom .title ul li {
        float: left;
        padding: 10px 15px;
        list-style: none;
        border: 1px solid #e3e3e3;
        margin: 0px -1px -1px 0px;
    }

    .perssiom .title ul li a {
        color: #333;
        font-size: 13px;
        font-weight: 600;
    }

    .perssiom .items .iitem li {
        list-style: none;
    }

    .iitem > ul > li {
        border: 1px solid #eee;
        line-height: 35px;
    }

    .iitem table {
        border-collapse: collapse;
        width: 100%;
    }

    .iitem table tr td {
        border: 1px solid #e3e3e3;
        line-height: 25px;
        padding: 10px 5px;
    }
    .iitem table tr td label{
       font-size:12px;
       font-weight:normal;
    }
    .iitem table tr td input.l{
       margin:5px 5px 0px 0px;
    }
    .iitem table tr td li {
        width: 14%;
        float: left;
    }

    .bottom_bar {
        text-align: right;
        background-color: #eee;
        padding-right: 10px;
        height: 32px;
        line-height: 32px;

    }

</style>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1><?=lang('menu_yhgl')?></h1>
          <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-pencil"></i>权限设置</h3>
          </div>
          <div class="panel-body">
            <form action="<?= blang_url('users/member/SetPermission') ?>" method="post">
                <div class="perssiom">
                    <div class="title">
                        <ul>

                            <?php foreach ($tree as $key => $row) { ?>
                                <li>
                                    <a href="javascript:void(0)">
                                        <?= $row['title'] ?>
                                    </a>
                                </li>
                            <?php } ?>

                        </ul>
                    </div>
                    <div style="clear:both;"></div>
                    <div class="items">
                        <?php foreach ($tree as $key => $row) { ?>
                            <div class="iitem" style="display:none;">

                                <table>
                                    <tr>
                                        <td rowspan="<?= intval(count($row['children'])) + 1 ?>" style="width:10%">
                                            <label class="l">
                                                <input type="checkbox" name="ids[]"
                                                   data-id="<?= $row['id'] ?>"
                                                   value="<?= $row['id'] ?>" 
                                                   <?= in_array($row['id'], $roleInfo['permission']) ? 'checked' : '' ?>
                                                   class="cbk l">

                                                <?= $row['title'] ?>
                                            </label>
                                            <div class="clear"></div>
                                        </td>
                                    </tr>
                                    <?php if ($row['children']) {
                                        foreach ($row['children'] as $children) { ?>
                                            <tr>

                                                <td style="width:10%">
                                                    <label class="l">
                                                        <input type="checkbox"
                                                           data-id="<?= $row['id'].'_'.$children['id'] ?>"
                                                           name="ids[]"
                                                           value="<?= $children['id'] ?>" 
                                                           <?= in_array($children['id'], $roleInfo['permission']) ? 'checked' : '' ?>
                                                           class="cbk l">

                                                        <?= $children['title'] ?>
                                                    </label>
                                                    <div class="clear"></div>
                                                </td>
                                                <td style="width:70%;padding: 0;margin: 0;">
                                                    <?php if ($children['children']) {
                                                        foreach ($children['children'] as $grandson) { ?>
                                                    <table>
                                                        <tr>
                                                            <td rowspan="<?= intval(count($grandson['children'])) + 1 ?>" style="width:16%">
                                                                <label class="l">
                                                                    <input type="checkbox"
                                                                       data-id="<?= $row['id'].'_'.$children['id'].'_'.$grandson['id'] ?>"
                                                                       name="ids[]"
                                                                       value="<?= $grandson['id'] ?>" 
                                                                       <?=
                                                                in_array($grandson['id'], $roleInfo['permission']) ? 'checked' :
                                                                    '' ?>
                                                                       class="cbk l">
                                                                    
                                                                    <?= $grandson['title'] ?>
                                                                </label>
                                                                <div class="clear"></div>
                                                            </td>
                                                            <td>
                                                                <ul>
                                                                    <?php if ($grandson['children']) {
                                                                    foreach ($grandson['children'] as $grandson2) { ?>
                                                                        <li>
                                                                            <label class="l">
                                                                                <input type="checkbox"
                                                                                   data-id="<?= $row['id'].'_'.$children['id'].'_'.$grandson['id'].'_'.$grandson2['id'] ?>"
                                                                                   name="ids[]"
                                                                                   value="<?= $grandson2['id'] ?>" 
                                                                                   <?=
                                                                            in_array($grandson2['id'], $roleInfo['permission']) ? 'checked' :
                                                                                '' ?>
                                                                                   class="cbk l">
                                                                                
                                                                                <?= $grandson2['title'] ?>
                                                                            </label>
                                                                            <div class="clear"></div>
                                                                        </li>

                                                                    <?php }
                                                                } ?>
                                                                </ul>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                        <?php }
                                                    } ?>

                                                    
                                                    

                                                </td>

                                            </tr>

                                        <?php }
                                    } ?>
                                </table>
                            </div>
                        <?php } ?>
                    </div>
                </div>

                <!-- contents end -->

                <div class="bottom_bar">
                    <a href="javascript:void(0)" onclick="javascript:history.go(-1)"
                       style="    float: left;padding-left: 5px;color: #333;"> << 返回</a>
                    <span>权限组：【<?= $roleInfo['name_en'] ?>】</span>

                    <input type="hidden" name="id" value="<?= $roleInfo['id'] ?>">
                    <input type="hidden" name="update" value="1">
                    <input class="btn-submit" type="submit" id="btn-submit" value=" 提 交 ">
                </div>
            </form>
          </div>
        </div>
    </div>
  
    <script type="text/javascript">
    $('.items>.iitem').eq(0).show();
    $('.perssiom .title li').eq(0).css('background-color', '#ddd');
    $('.perssiom .title li').click(function () {
        var cur_index = $('.perssiom .title li').index(this)
        $('.iitem').eq(cur_index).show();
        $('.iitem').not($('.iitem').eq(cur_index)).hide();


        $('.perssiom .title li').eq(cur_index).css('background-color', '#ddd');
        $('.perssiom .title li').not($('.perssiom .title li').eq(cur_index)).css('background-color', '#fff');

    });


    //父级的菜单选中，默认子类全部选中
    $('.cbk').click(function () {
        var ids = $(this).data('id').toString();

        var parentsId = [];
        ids.split('_').forEach(function(id, index, arr){
            parentsId.push(id);
        });
        parentsId.pop();
        $('input[data-id^="'+ids+'_"]').prop('checked', $(this).prop('checked'));
        for(var i in parentsId){
            var id = parentsId.slice(0, parseInt(i) + 1).join('_');
            var $parent = $('input[data-id="'+id+'"]');
            if($('input:checked[data-id^="'+id+'_"]').length === 0){
                $parent.prop('checked', false);
            }else{
                $parent.prop('checked', true);
            }
        }
    });
</script>
</div>